package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.dto.CustomerContactDTO;
import com.prd.mapper.CustomerContactMapper;
import com.prd.model.CustomerContactDO;
import com.prd.service.CustomerContactService;
import com.prd.tools.CopyNonNullBeanUtils;
import com.prd.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户-客户来源，关系人等
 * </p>
 *
 * @author o1
 * @since 2023-11-26
 */
@Service("CustomerContactService")
public class CustomerContactServiceImpl extends ServiceImpl<CustomerContactMapper, CustomerContactDO> implements CustomerContactService {

    @Autowired
    private CustomerContactMapper contactMapper;

    @Override
    public JsonData uAdd(CustomerContactDTO contactDTO) {
        String guestNo = String.valueOf(contactDTO.hashCode());
        LambdaQueryWrapper<CustomerContactDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerContactDO::getNo, contactDTO.getNo());
        lambdaQueryWrapper.eq(CustomerContactDO::getGuestNo, guestNo);
        CustomerContactDO contactDO = contactMapper.selectOne(lambdaQueryWrapper);
        if (contactDO == null) {
            contactDO = new CustomerContactDO();
            BeanUtils.copyProperties(contactDTO, contactDO);
            contactDO.setGuestNo(guestNo);
            contactMapper.insert(contactDO);
            return JsonData.buildSuccess(contactDO);
        }else {
            return JsonData.buildError("已有同客户下相同联系人");
        }
    }

    @Override
    public JsonData uUpdateAndAdd(CustomerContactDTO contactDTO) {
        String guestNo = String.valueOf(contactDTO.hashCode());
        LambdaQueryWrapper<CustomerContactDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerContactDO::getGuestNo, guestNo);
        if (contactDTO.getNo() != null)
            lambdaQueryWrapper.eq(CustomerContactDO::getNo, contactDTO.getNo());
        CustomerContactDO projectDO = contactMapper.selectOne(lambdaQueryWrapper);
        if (projectDO == null) {
            projectDO = new CustomerContactDO();
            BeanUtils.copyProperties(contactDTO, projectDO);
            projectDO.setGuestNo(guestNo);
            contactMapper.insert(projectDO);
        }else {
            CopyNonNullBeanUtils.copyProperties(contactDTO, projectDO);
            contactMapper.update(projectDO, lambdaQueryWrapper);
        }
        return JsonData.buildSuccess(projectDO);
    }

    @Override
    public JsonData addSimple(CustomerContactDO contactDO) {
        CustomerContactDO contactDB = new CustomerContactDO();
        BeanUtils.copyProperties(contactDO, contactDB);
        contactMapper.insert(contactDB);
        return JsonData.buildSuccess(contactDO);
    }

    @Override
    public JsonData uUpdate(CustomerContactDTO contactDTO) {
        int customerNo = contactDTO.hashCode();
        LambdaQueryWrapper<CustomerContactDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerContactDO::getGuestNo, customerNo);
        lambdaQueryWrapper.eq(CustomerContactDO::getType, contactDTO.getType());
        CustomerContactDO projectDO = contactMapper.selectOne(lambdaQueryWrapper);
        //不存在项目id
        if (projectDO == null) {
            return JsonData.buildError("未在该客户下相同联系人");
        }else {
            CopyNonNullBeanUtils.copyProperties(contactDTO, projectDO);
            contactMapper.updateById(projectDO);
            return JsonData.buildSuccess(projectDO);
        }
    }

    @Override
    public JsonData update(CustomerContactDO contactDO) {
        LambdaQueryWrapper<CustomerContactDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CustomerContactDO::getGuestNo, contactDO.getGuestNo());
        lambdaQueryWrapper.eq(CustomerContactDO::getType, contactDO.getType());
        CustomerContactDO contactDB = contactMapper.selectOne(lambdaQueryWrapper);
        //不存在项目id
        if (contactDB == null) {
            return JsonData.buildError("未在该客户下相同联系人");
        }else {
            CopyNonNullBeanUtils.copyProperties(contactDO, contactDB);
            contactMapper.updateById(contactDB);
            return JsonData.buildSuccess(contactDB);
        }
    }

    @Override
    public Map<String, Object> uContactList(int page, int size, String customerNo, int projectId, int type) {
        LambdaQueryWrapper<CustomerContactDO> wrapper = new LambdaQueryWrapper<CustomerContactDO>();
        if (customerNo != null) {
            wrapper.eq(CustomerContactDO::getNo, customerNo);
        }
        if (projectId != -1)
            wrapper.eq(CustomerContactDO::getProjectId, projectId);
        if (type != -1)
            wrapper.eq(CustomerContactDO::getType, type);
        Page<CustomerContactDO> pageInfo = new Page<>(page, size);
        IPage<CustomerContactDO> CustomerContactDOIPage = contactMapper.selectPage(pageInfo,
                wrapper);
        Map<String, Object> map = new HashMap<>(3);
        map.put("total_record", CustomerContactDOIPage.getTotal());
        map.put("total_page", CustomerContactDOIPage.getPages());
        map.put("current_data", CustomerContactDOIPage.getRecords().stream().map(
                        this::doToDto
                ).collect(Collectors.toList())
        );
        return map;
    }


    @Override
    public List<CustomerContactDTO> uSearchForList(String no, int type) {
        LambdaQueryWrapper<CustomerContactDO> wrapper = new LambdaQueryWrapper<CustomerContactDO>();
        if (no != null) {
            wrapper.eq(CustomerContactDO::getNo, no);
        }
        if (type != -1)
            wrapper.eq(CustomerContactDO::getType, type);
        List<CustomerContactDO> customerContactDOList = contactMapper.selectList(wrapper);
        return customerContactDOList.stream().map(this::doToDto).collect(Collectors.toList());
    }


    private CustomerContactDTO doToDto(CustomerContactDO contactDO) {
        CustomerContactDTO contactDTO = new CustomerContactDTO();
        BeanUtils.copyProperties(contactDO, contactDTO);
        return contactDTO;
    }
}
